/*
 author:王立雍
 date:2014-08-14 19:07:00
 desc:jquery扩展模板切换组件
 */
(function($){
    $.fn.themesChoice = function(os){
        var options = {//默认分页参数设置
            pageNo : 0,
            themesChoiceList :{},
            contentWidth :0,
            themesNo : 0,
            themesLength : 0,
            themesPage : 0,
            pageWidth : 0,
            container:$(this)
        };
        var o = $.extend(options, os );
        $.themes.init(o);
        $.themes.handler(o);

    }
    $.themes = {
        init:function(o){
            o.pageNo = 0;
            o.themesList = o.container.find(".themes-center .themes-list");
            o.contentWidth =  $(window).innerWidth()-50;
            $(".themes-center").css({width: o.contentWidth});
            o.themesNo = parseInt(o.contentWidth / o.themesList.find("li").outerWidth());
            o.themesLength = o.themesList.children("li").length;
            o.themesPage = o.themesLength % o.themesNo == 0 ? o.themesLength / o.themesNo : parseInt(o.themesLength/ o.themesNo) + 1;
            o.pageWidth = o.themesList.children("li").outerWidth() * o.themesNo;
            o.themesList.css({width: o.themesPage* o.pageWidth});
        },
        handler:function(o){
            o.container.hover(function(){
                var prev = $(this).find(".themes-prev").is("animated");
                var next = $(this).find(".themes-next").is("animated");
                if(o.themesLength > o.themesNo){
                    $(this).find(".themes-prev").stop().animate({left:0},200);
                    $(this).find(".themes-next").stop().animate({right:0},200);
                }
            },function(){
                    $(this).find(".themes-prev").stop().animate({left:-50},200);
                    $(this).find(".themes-next").stop().animate({right:-50},200);
            });

            o.container.find(".themes-prev").live("click",function(){
                if(o.pageNo>0){
                    o.pageNo-=1;
                    o.themesList.animate({left: o.pageNo*-o.pageWidth});
                }
            });
            o.container.find(".themes-next").live("click",function(){
                if(o.pageNo< o.themesPage-1){
                    o.pageNo+=1;
                    o.themesList.animate({left: o.pageNo*-o.pageWidth});
                }
            });

        }
    }
})(jQuery);